﻿using Gargoyle.Roton;
using System;
using System.Collections.Generic;
using System.Text;

namespace Gargoyle.ZZT
{
    public delegate bool ConditionDelegate(Game g, Thing thing, Pointer offset, ref bool result);

    static class Conditions
    {
        static public bool Alligned(Game g, Thing thing, Pointer offset, ref bool result)
        {
            Thing player = g.Things[0];
            result = ((thing.Location.X == player.Location.X) || (thing.Location.Y == player.Location.Y));
            return true;
        }

        static public bool Any(Game g, Thing thing, Pointer offset, ref bool result)
        {
            Tile kind = new Tile();
            bool success = g.ReadKind(thing, offset, ref kind);

            if (success)
            {
                Location location = new Location(0, 0);
                result = g.FindTile(kind, location);
            }
            return success;
        }

        static public bool Blocked(Game g, Thing thing, Pointer offset, ref bool result)
        {
            Vector vector = new Vector(0, 0);
            if (g.ReadDirection(thing, offset, vector))
            {
                Element element = g.GetElementAt(thing.Location.Add(vector));
                result = !element.Floor;
                return true;
            }
            return false;
        }

        static public bool Contact(Game g, Thing thing, Pointer offset, ref bool result)
        {
            Thing player = g.Things[0];
            int distanceX = thing.Location.X - player.Location.X;
            int distanceY = thing.Location.Y - player.Location.Y;
            result = ((distanceX * distanceX) + (distanceY * distanceY) == 1);
            return true;
        }

        static public bool Energized(Game g, Thing thing, Pointer offset, ref bool result)
        {
            result = (g.Info.EnergyCycles > 0);
            return true;
        }

        static public bool Not(Game g, Thing thing, Pointer offset, ref bool result)
        {
            bool success = g.ReadCondition(thing, offset, ref result);
            result = !result;
            return success;
        }

    }
}
